﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Threading;

using AJRavindiran.Jolt.RuneScape.Models.Characters;
using AJRavindiran.Jolt.Utilities;

namespace AJRavindiran.Jolt.RuneScape.Workers
{
    /// <summary>
    /// Represents a major updates worker.
    /// </summary>
    public class MajorUpdatesWorker
    {
        #region Fields
        /// <summary>
        /// The invterval between each update. This is a major update, so the time 
        /// has to be fairly fast. 600ms is the normal update interval for runescape.
        /// </summary>
        private const int UPDATE_INTERVAL = 600;
        /// <summary>
        /// Stores the last update time. This is then calculated to see if 600mb has
        /// passed by, so we can verify we need to do another update.
        /// </summary>
        private int lastUpdate;
        private Thread majorUpdatesThread;
        #endregion Fields

        #region Constructors
        /// <summary>
        /// Constructs a new major udpates worker class.
        /// </summary>
        public MajorUpdatesWorker()
        {
            Jolt.GetLog().WriteInfo("Constructing major updates worker...");

            // Initialize the worker thread, set the highest priority, and start it.
            this.majorUpdatesThread = new Thread(new ThreadStart(Run));
            this.majorUpdatesThread.Priority = ThreadPriority.Highest;
            this.majorUpdatesThread.Name = "MajorUpdatesThread";
        }
        #endregion Constructors

        #region Methods
        /// <summary>
        /// A run processing all major updates (player/npc updates).
        /// </summary>
        private void Run()
        {
            this.lastUpdate = Environment.TickCount;
            while (true)
            {
                //RuneScape.GetPacketManager().ParseImconingPackets();

                if (Environment.TickCount - lastUpdate >= UPDATE_INTERVAL)
                {
                    lastUpdate = Environment.TickCount;
                }
                try
                {
                    Thread.Sleep(100);
                }
                catch (Exception ex) { Jolt.GetLog().WriteException(ex); }
            }
        }

        /// <summary>
        /// Processes character related major updates.
        /// </summary>
        public void ProcessCharacters()
        {
            foreach (Character c in RuneScape.GetCharacterManager().GetCharacters().Values)
            {
                c.Tick();
                c.GetMovement().GetNextMovement();
            }

            foreach (Character c in RuneScape.GetCharacterManager().GetCharacters().Values)
            {
                if (!c.GetStatus().Online)
                {
                    RuneScape.GetCharacterManager().UnRegister(c);
                }
                else
                {
                    RuneScape.GetPacketManager().GetBuilder().BuildCharacterUpdates(c);
                }
            }

            foreach (Character c in RuneScape.GetCharacterManager().GetCharacters().Values)
            {
                c.GetUpdateFlags().ClearFlags();
                c.GetSocket().WriteBuffer();
            }
        }

        /// <summary>
        /// Get accessor for the major updates worker thread.
        /// </summary>
        /// <returns></returns>
        public Thread GetThread()
        {
            return this.majorUpdatesThread;
        }
        #endregion Methods
    }
}
